/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.financeiro.view;

import br.jm.financeiro.base.eao.EaoException;
import br.jm.financeiro.datamodel.LazyDiagnostico;
import br.jm.financeiro.entity.Diagnostico;
import br.jm.financeiro.entity.DiagnosticoNivel1;
import br.jm.financeiro.sistema.eao.DiagnosticoEao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author joao
 */
public class DiagnosticoBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private Diagnostico selectedDiagnostico;
    private DiagnosticoNivel1 selectedDiagnosticoNivel1;
    private LazyDataModel<Diagnostico> lazyModel;
    private final DiagnosticoEao diagnosticoEAO;
    private boolean salvar;
    private FacesMessage msg;
    private boolean altera;
    private int rowid;

    public DiagnosticoBean() {
        diagnosticoEAO = DiagnosticoEao.getInstance();

        selectedDiagnostico = new Diagnostico();
        selectedDiagnosticoNivel1 = new DiagnosticoNivel1();
        this.altera = true;

        lazyModel = new LazyDiagnostico(diagnosticoEAO);
    }

    public void inserir(ActionEvent actionEvent) throws EaoException {
        RequestContext context = RequestContext.getCurrentInstance();
        boolean loggedIn = false;
        msg = null;

        try {
            if (this.selectedDiagnostico.getDscDiagnostico().isEmpty()) {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Obrigatório Digitar o Nome do Diagnóstico!", null);
            } else if (this.selectedDiagnostico.getDiagnosticoNivel1List().isEmpty()) {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Obrigatório Inserir Descrição!", null);
            } else {
                selectedDiagnostico.setIdDiagnostico(null);
                diagnosticoEAO.create(selectedDiagnostico);
                
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Diagnostico Inserido com Sucesso: ".concat(this.selectedDiagnostico.getDscDiagnostico())), this.selectedDiagnostico.getDscDiagnostico());
                loggedIn = true;
            }
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao Inserir"  , this.selectedDiagnostico.getDscDiagnostico());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);

            if (loggedIn) {
                this.selectedDiagnostico = new Diagnostico();
            }

            context.addCallbackParam("loggedIn", loggedIn);
        }
    }

    public void alterar(ActionEvent actionEvent) throws EaoException {
        RequestContext context = RequestContext.getCurrentInstance();
        boolean loggedIn = false;
        msg = null;

        try {
            if (this.selectedDiagnostico.getDscDiagnostico().isEmpty()) {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Obrigatório Digitar o Nome do Diagnóstico!", null);
            } else if (this.selectedDiagnostico.getDiagnosticoNivel1List().isEmpty()) {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Obrigatório Inserir Descrição!", null);
            } else {

                diagnosticoEAO.edit(selectedDiagnostico);

                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Diagnostico Alterado com Sucesso: ".concat(this.selectedDiagnostico.getDscDiagnostico())), selectedDiagnostico.getDscDiagnostico());
                loggedIn = true;

            }
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao Alterar" + selectedDiagnostico.getDscDiagnostico(), e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);

            if (loggedIn) {
                this.selectedDiagnostico = new Diagnostico();

                context.update("frmDiagnostico:panel");
                context.update("frmDiagnostico:tabDiagnostico");
            }

            context.update("frmDiagnostico:messages");

            context.addCallbackParam("loggedIn", loggedIn);
        }
    }

    public void excluir(ActionEvent actionEvent) {
        msg = null;

        try {
            diagnosticoEAO.destroy(selectedDiagnostico);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Diagnostico Excluida com Sucesso: ".concat(this.selectedDiagnostico.getDscDiagnostico())), selectedDiagnostico.getDscDiagnostico());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Excluir: " + selectedDiagnostico.getDscDiagnostico(), e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void preparaInserir(ActionEvent actionEvent) {
        this.salvar = true;
        List<DiagnosticoNivel1> DiagnosticoNivel1List = new ArrayList<DiagnosticoNivel1>();
        this.selectedDiagnostico = new Diagnostico();
        this.selectedDiagnostico.setDiagnosticoNivel1List(DiagnosticoNivel1List);
    }

    public void preparaInserirDescricao(ActionEvent actionEvent) {
        this.altera = true;
        this.selectedDiagnosticoNivel1 = new DiagnosticoNivel1();
    }

    public Diagnostico getSelectedDiagnostico() {
        return selectedDiagnostico;
    }

    public void setSelectedDiagnostico(Diagnostico selectedDiagnostico) {
        this.selectedDiagnostico = selectedDiagnostico;
    }

    public LazyDataModel<Diagnostico> getLazyModel() {
        return lazyModel;
    }

    public void setLazyModel(LazyDataModel<Diagnostico> lazyModel) {
        this.lazyModel = lazyModel;
    }

    public boolean isSalvar() {
        return salvar;
    }

    public void setSalvar(boolean salvar) {
        this.salvar = salvar;
    }

    public DiagnosticoNivel1 getSelectedDiagnosticoNivel1() {
        return selectedDiagnosticoNivel1;
    }

    public void setSelectedDiagnosticoNivel1(DiagnosticoNivel1 selectedDiagnosticoNivel1) {
        this.selectedDiagnosticoNivel1 = selectedDiagnosticoNivel1;
    }

    public boolean isAltera() {
        return altera;
    }

    public void setAltera(boolean altera) {
        this.altera = altera;
    }

    public int getRowid() {
        return rowid;
    }

    public void setRowid(int rowid) {
        this.rowid = rowid;
    }

    public void inserirDescricao(ActionEvent actionEvent) throws EaoException {
        msg = null;

        try {
            selectedDiagnosticoNivel1.setIdDiagnostico(selectedDiagnostico);
            this.selectedDiagnostico.getDiagnosticoNivel1List().add(selectedDiagnosticoNivel1);
            
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Descrição Inserida com Sucesso", this.selectedDiagnosticoNivel1.getDscDiagnosticoNivel1());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao Inserir" + this.selectedDiagnosticoNivel1.getDscDiagnosticoNivel1(), e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
            
            this.selectedDiagnosticoNivel1 = new DiagnosticoNivel1();
        }
    }

    public void alterarDescricao(ActionEvent actionEvent) throws EaoException {
        msg = null;

        try {
            selectedDiagnosticoNivel1.setIdDiagnostico(selectedDiagnostico);
            this.selectedDiagnostico.getDiagnosticoNivel1List().set(this.rowid, selectedDiagnosticoNivel1);
            
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Descrição Alterar com Sucesso", this.selectedDiagnosticoNivel1.getDscDiagnosticoNivel1());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao Alterar" + this.selectedDiagnosticoNivel1.getDscDiagnosticoNivel1(), e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
            
            this.selectedDiagnosticoNivel1 = new DiagnosticoNivel1();
        }
    }

    public void excluirDescricao(ActionEvent actionEvent) {
        msg = null;

        try {
            this.selectedDiagnostico.getDiagnosticoNivel1List().remove(selectedDiagnosticoNivel1);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Descrição Excluida com Sucesso", this.selectedDiagnosticoNivel1.getDscDiagnosticoNivel1());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao Excluir: " + this.selectedDiagnosticoNivel1.getDscDiagnosticoNivel1(), e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
            
            this.selectedDiagnosticoNivel1 = new DiagnosticoNivel1();
        }
    }
}
